  #include "screen_part.h"
#include "ui_screen_part.h"

Screen_part::Screen_part(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Screen_part)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_QuitOnClose,false);

    memset(list1, 0, sizeof(list1));
    memset(list2, 0, sizeof(list2));
    memset(list3, 0, sizeof(list3));
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("序号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("项目"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("无"));
    ui->tableWidget->setHorizontalHeaderItem(3,new QTableWidgetItem("有时"));
    ui->tableWidget->setHorizontalHeaderItem(4,new QTableWidgetItem("经常"));

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //列宽自适应内容
//    ui->tableWidget->resizeColumnsToContents();
//      //隐藏表格的列标号
    ui->tableWidget->verticalHeader()->setVisible(false);

    //设置表格不可编辑
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setRowCount(42);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);


    sql = QSqlDatabase::addDatabase("QSQLITE", "fifth");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }

    connect(ui->pushButton_sub, SIGNAL(clicked(bool)), this, SLOT(calulate()));
}


void Screen_part::Show_Rows()
{
    QFile file("test.txt");
    file.open(QIODevice::ReadWrite);
    QTextStream str_in(&file);
    QString text;

    QCheckBox* btn1 = NULL;
    QCheckBox* btn2 = NULL;
    QCheckBox* btn3 = NULL;
    int i = 0;
    while(1)//依次取出下一行数据
    {
        text = str_in.readLine(100000);
        if(text.isEmpty() || text.isNull()) break;

        btn1 = new QCheckBox(this);
        btn2 = new QCheckBox(this);
        btn3 = new QCheckBox(this);


        btn1->setText("从未");
        btn2->setText("有时");
        btn3->setText("经常");

        QButtonGroup* group = new QButtonGroup(this);
        group->addButton(btn1);
        group->addButton(btn2);
        group->addButton(btn3);
        group->setExclusive(true);

        box1[i] = btn1;
        box2[i] = btn2;
        box3[i] = btn3;

        //将查询到的数据插入到表格里
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i + 1)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(text));

        ui->tableWidget->setCellWidget(i,2, btn1);
        ui->tableWidget->setCellWidget(i,3, btn2);
        ui->tableWidget->setCellWidget(i,4, btn3);
        i++;
    }
}


void Screen_part::do_test(int id, QString name){
//    QSqlQuery query(sql);
    qDebug() << id << name;
    this->id = id;
    ui->label_id->setText("儿童ID: " + QString::number(id) + ", " + name);
    Show_Rows();
    this->show();
}


void Screen_part::calulate(){
    int sum[41] = {0};
    for(int i = 0; i < 41; i++){
        if(box1[i]->isChecked() == true){
            sum[i] = 0;
        }
        if(box2[i]->isChecked() == true){
            sum[i] = 1;
        }
        if(box3[i]->isChecked() == true){
            sum[i] = 2;
        }
    }
//    躯体化/惊恐= 1+6+9+12+15+18+19+22 +24+27 +30 +34+38
//    广泛性焦虑=5+7+14+21+23 +28 +33+35+37
//    分离性焦虑=4+8+13+16+20+25 +29+31
//    社交恐怖= 3+ 10+26+32 +39 +40+41
//    学校恐怖=2+11+17+36
//    总分=all
    int score1_list[] = {1, 6, 9, 12, 15, 18, 19, 22, 24, 27, 30, 34, 38};
    int score2_list[] = {5, 7, 14, 21, 23, 28, 33, 35, 37};
    int score3_list[] = {4, 8, 13, 16, 20, 25, 29, 31};
    int score4_list[] = {3, 10, 26, 32, 39, 40, 41};
    int score5_list[] = {2, 11, 17, 36};
    int score_all[6] = {0, 0, 0, 0 ,0, 0};
    for(int i = 0; i < sizeof(score1_list) / sizeof(score1_list[0]); i++){
        score_all[0] += sum[score1_list[i] - 1];
    }
    for(int i = 0; i < sizeof(score2_list) / sizeof(score2_list[0]); i++){
        score_all[1] += sum[score2_list[i] - 1];
    }
    for(int i = 0; i < sizeof(score3_list) / sizeof(score3_list[0]); i++){
        score_all[2] += sum[score3_list[i] - 1];
    }
    for(int i = 0; i < sizeof(score4_list) / sizeof(score4_list[0]); i++){
        score_all[3] += sum[score4_list[i] - 1];
    }
    for(int i = 0; i < sizeof(score5_list) / sizeof(score5_list[0]); i++){
        score_all[4] += sum[score5_list[i] - 1];
    }
    score_all[5] = score_all[0] + score_all[1] + score_all[2] + score_all[3] + score_all[4];
    for(int i = 0; i < 6; i++){
        qDebug() << "score = " << score_all[i];
    }


    QString test_date = QDate::currentDate().toString();
    QFile file("com.txt");
    file.open(QIODevice::ReadWrite);
    QTextStream stream(&file);
    QString com;
    stream >> com;
    QString addr = com;
    QString conclusion = "NULL";
    QString suggest = "NULL";
    int result = 0;

    QSqlQuery query(sql);

    QString str;
    str = "INSERT INTO tb_result(child_id, test_date, addr, score1, score2, score3, score4, score5, total, conclusion, suggest, result) VALUES ";
    str += "(" + QString::number(id) + ",";
    str += "'" + test_date + "',";
    str += "'" + addr + "',";
    str += QString::number(score_all[0]) + ",";
    str += QString::number(score_all[1]) + ",";
    str += QString::number(score_all[2]) + ",";
    str += QString::number(score_all[3]) + ",";
    str += QString::number(score_all[4]) + ",";
    str += QString::number(score_all[5]) + ",";
    str += "'" + conclusion + "',";
    str += "'" + suggest + "',";
    str += QString::number(result) + ");";
    qDebug() << str;

    bool ok = query.exec(str);
    if(ok == true){
        QMessageBox::information(this, "插入成功", "插入成功", "确认", "取消");
    }
    else{
        QMessageBox::warning(this, "插入失败", "插入失败", "确认", "取消");
    }
//    QString birth;
//    QString name;
//    QString birth_sql = "SELECT birth,name FROM child_data WHERE id=" + QString::number(id);
//    query.exec(birth_sql);
//    query.next();
//    birth = query.value(0).toString();
//    name = query.value(1).toString();
    emit Guidance(id);
    this->close();
}

Screen_part::~Screen_part()
{
    delete ui;
}
